package online.longzipeng.cinemasys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import online.longzipeng.cinemasys.common.annotion.NeedPermissions;
import online.longzipeng.cinemasys.common.exception.AppException;
import online.longzipeng.cinemasys.common.result.ResponseEnum;
import online.longzipeng.cinemasys.common.result.Result;
import online.longzipeng.cinemasys.dto.sysuser.SysUserLoginResponseDTO;
import online.longzipeng.cinemasys.dto.sysuser.SysUserQueryResponseDTO;
import online.longzipeng.cinemasys.entity.SysUser;
import online.longzipeng.cinemasys.service.SysUserService;
import online.longzipeng.cinemasys.vo.sysuser.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzp
 * @since 2021-04-08
 */
@RestController
@RequestMapping("/sys-user")
@Api(value = "系统用户相关接口",description = "系统用户相关接口",tags = "系统用户相关接口")
public class SysUserController {

    @Autowired
    SysUserService sysUserService;

    @PostMapping("/get-list-page")
    @ApiOperation("查询所有用户带分页")
    public Result getListPage(@RequestBody SysUserQueryVO sysUserQueryVO){
        IPage<SysUserQueryResponseDTO> users = sysUserService.getListPage(sysUserQueryVO);
        return Result.success(users);
    }

    @GetMapping("/get-by-id")
    @ApiOperation("通过id查询用户")
    public Result getById(@RequestParam(value = "id",required = true)Integer id){
        return Result.success(sysUserService.getById(id));
    }

    @GetMapping("/info")
    @ApiOperation("获取用户信息")
    public Result getInfo(@RequestParam(value = "token",required = true) String token){
        SysUserLoginResponseDTO sysUserLoginResponseDTO = sysUserService.getInfo(token);
        sysUserLoginResponseDTO.setToken(token);
        return Result.success(sysUserLoginResponseDTO);
    }

    @GetMapping("/user-info")
    @ApiOperation("获取用户信息")
    public Result getUserInfo(@RequestParam(value = "token",required = true) String token){
        SysUser sysUser = sysUserService.getUserInfo(token);
        return Result.success(sysUser);
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result login(@Valid @RequestBody SysUserLoginVO sysUserLoginVO, HttpServletRequest httpServletRequest){
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserLoginVO,sysUser);
        sysUser.setLastLoginIp(httpServletRequest.getRemoteAddr());
        SysUserLoginResponseDTO sysUserLoginResponseDTO = sysUserService.login(sysUser);
        return Result.success(sysUserLoginResponseDTO);
    }

    @PostMapping("/change_password")
    @ApiOperation("修改用户密码")
    public Result changePassword(@Valid @RequestBody SysUserChangePasswordVo sysUserChangePasswordVo){
        sysUserService.changePassword(sysUserChangePasswordVo);
        return Result.success(null,"密码修改成功！！请重新登录！！");
    }

    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public Result logOut(@ApiParam(value = "用户id",required = true)Long id){
        return Result.success();
    }

    @PostMapping("/save")
    @NeedPermissions("sysUser:save")
    @ApiOperation("新增管理端用户")
    public Result save(@RequestBody SysUserSaveVO sysUserSaveVO){
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserSaveVO,sysUser);
        sysUserService.add(sysUser);
        return Result.success(null,"用户添加成功！！");
    }

    @GetMapping("/delete-one")
    @NeedPermissions("sysUser:delete")
    @ApiOperation("删除管理端用户")
    public Result deleteOne(@ApiParam(value = "用户id",required = true)Long id){
        try {
            sysUserService.removeById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"用户删除成功！！");
    }

    @GetMapping("/delete")
    @NeedPermissions("sysUser:delete")
    @ApiOperation("删除多个管理端用户")
    public Result deleteOne(@ApiParam(value = "用户id数组",required = true)Long[] ids){
        List<Long> idList = Arrays.stream(ids).collect(Collectors.toList());
        try {
            sysUserService.removeByIds(idList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"用户删除成功！！");
    }

    @PostMapping("/update")
    @NeedPermissions("sysUser:update")
    @ApiOperation("修改管理端用户")
    public Result update(@Valid @RequestBody SysUserUpdateVO sysUserUpdateVO) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserUpdateVO,sysUser);
        sysUserService.updateUser(sysUser);
        return Result.success(null,"用户修改成功！！");
    }

}
